package com.wangyg.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyg6 on 2019/5/29 10:11.
 */
public class Publisher implements Observerable {
	private List<Observer> observers = new ArrayList<>();
	private String msg;

	@Override
	public void register(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void remove(Observer observer) {
		observers.remove(observer);
	}

	@Override
	public void publish() {
		observers.forEach(x -> x.recieve(msg));
	}

	//接收消息，推送到观察者
	public void acceptMsg(String msg) {
		this.msg = msg;
		System.out.println("发布者发布消息:" + msg);
		publish();
	}
}
